<?php
session_start();
require_once( 'student_db.php' );
 
$m_db = new M_Student();
$user_id = $_SESSION['userid'];
$Year = 2557;
if (isset($_POST['m'])  &&  $_POST['m']!=""){

    if ($_POST['m']== 'load'){
	  $cid = $_POST['cid'];
	  $r = $_POST['r'];
	  $s = $_POST['s'];
	  $n = $_POST['n'];
	  $co = $_POST['co'];
	  
      $result=$m_db->loadStudentMove_Exit($co,$n,$cid,$r,$s);
      $rows=$m_db->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
			$today = date("Y-m-d");	
			$year = date("Y");
            while($row = $result->FetchRow()){

               $data[] = array(0,$row['student_id'],$row['status'],$row['class_id'],$row['code'], $row['title_name'] ." ". $row['thai_name'] . " " .$row['thai_surname']. " (".$row['thai_nickname'].")"  
			   ,$row['class_name'] .'/'. $row['room'] , $row['status_name'],$row['remark'],$row['updatedate'] ) ;
			   
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
     
    }else if ($_POST['m']=='exit'){ 
		$student_id=  $_POST['id'];
		$reason = $_POST['r'];
		$sid = $_POST['sid'];
		$m_db->updateStatusExit($student_id,$sid,$reason,$user_id );
		echo 1;
		
	}else if ($_POST['m']=='move'){
		$student_id=  $_POST['id'];
		$reason = $_POST['r'];
		$room = $_POST['room'];
		$reason = 'ย้ายห้อง : ' .$reason;
		$m_db->updateRoom($student_id, $room, $reason,$user_id);
		
	 echo 1;
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		$list = $_POST['list'];
		foreach($list as $val){
			$ar['student_id'] = $val['sid'];
			$ar['class_id']=$val['cid'];
			$ar['room']=$val['r'];
			$ar['year']=  $Year;
			
			$m_db->updateStudentStatus($val['sid'], 3, $user_id);
			$m_db->AddStudentToRoom($ar);
		}
		echo 1;
		
    }  
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if ($_GET['m'] == 'class'){
			echo json_encode( $m_db->getClassLevel());
		}else if ($_GET['m']=='room'){
			echo  json_encode( $m_db->load_all_room($_GET['i']));
		}else if ($_GET['m']=='status'){
			echo  json_encode($m_db->getStudentStatus());
		}
	} 
   
}

function DATEDIFF($date1, $date2){
	
}
 
?>